﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Platform.Storage;
using Avalonia.Styling;
using DocumentMaking.Lib.Models;
using FluentAvalonia.Styling;

namespace DocumentMaking.FluentNext.Dialogs;

public partial class SettingDialog : UserControl
{
    
    public SettingDialog(SettingModel? model)
    {
        InitializeComponent();
        ThemesComboBox.ItemsSource = new[] { "System", "Light", "Dark" };
        var s = Application.Current!.RequestedThemeVariant!.ToString();
        if (s == "Default") s = "System";
        ThemesComboBox.SelectedItem = s;
        FlowComboBox.ItemsSource = new[] { FlowDirection.LeftToRight, FlowDirection.RightToLeft };
        FlowComboBox.SelectedItem = FlowDirection;
        AppThemeComboBox.ItemsSource = new[] { "Next", "Carousel", "Setting", "TabStrip" };
        AppThemeComboBox.SelectedItem = model?.ThemeMode ?? "Next";
        OutputPathBox.Text = model?.DefaultOutputPath;
        OrganizeBox.Text = model?.DefaultOrganize;
    }

    private void ColorThemeSelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        if (ThemesComboBox.SelectedItem is not string s) return;
        var newTheme = GetThemeVariant(s);
        if (newTheme != null!)
            Application.Current!.RequestedThemeVariant = newTheme;
        var _faTheme = Application.Current!.Styles[0] as FluentAvaloniaTheme;
        _faTheme!.PreferSystemTheme = s == "System";
    }

    private void FlowSelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        if (FlowComboBox.SelectedItem is not FlowDirection value) return;
        var lifetime = Application.Current!.ApplicationLifetime;
        if (lifetime is IClassicDesktopStyleApplicationLifetime cdl)
        {
            if (cdl.MainWindow!.FlowDirection == value)
                return;
            cdl.MainWindow.FlowDirection = value;
        }
        else if (lifetime is ISingleViewApplicationLifetime single)
        {
            var mainWindow = TopLevel.GetTopLevel(single.MainView);
            if (mainWindow!.FlowDirection == value)
                return;
            mainWindow.FlowDirection = value;
        }
    }

    private ThemeVariant? GetThemeVariant(string value)
        => value switch
        {
            "Light" => ThemeVariant.Light,
            "Dark" => ThemeVariant.Dark,
            _ => null
        };


    private async void OpenClick(object? sender, RoutedEventArgs e)
    {
        var result = await TopLevel.GetTopLevel(this)!.StorageProvider
            .OpenFolderPickerAsync(new FolderPickerOpenOptions());
        if (result.Count == 0) return;
        OutputPathBox.Text = result[0].Path.LocalPath;
    }

    public SettingModel Done()
    {
        return new SettingModel()
        {
            ThemeMode = (string)AppThemeComboBox.SelectedItem!, DefaultOutputPath = OutputPathBox.Text,
            DefaultOrganize = OrganizeBox.Text
        };
    }
}